const app = getApp();
Page({
    data: {
        server:app.globalData.server,
        api:app.globalData.api,
        goodsList:{
            total:null,
            page: 1,
            list:[],
            show: true,
        },
        notData:false,
        searchKey:'',
        isLogin:false,
        token:'',
        dataEnd:false
    },
    onLoad(query) {
        this.setData({
            searchKey:query.key
        })
        let that = this;
        my.getStorage({
            key: 'userInfo',
            success: function(res) {
                that.setData({
                    isLogin:res.data?true:false,
                    token:res.data?res.data.token:'',
                    goodsList:{
                        total:null,
                        page: 1,
                        list:[],
                        show: true,
                    },
                    dataEnd:false
                })
                that.mySchedulde();
            }
        });
    },
    //接受子组件传递过来的数据
    onChange(key) {
        this.setData({
            searchKey:key,
            goodsList:{
                total:null,
                page: 1,
                list:[],
                show: true,
            },
            dataEnd:false
        })
        this.mySchedulde();
    },
    onShareAppMessage(){
      return {
        desc: '我刚刚跟你分享了一件好物，快来看看吧',
        path: 'page/index/index?pid=123&productId=1'  
      }
    },
    bindShare(){
        if(!this.data.isLogin){
        my.reLaunch({ url: '../login/login' });
        return false
        }else{
        this.onShareAppMessage()
        }
    },
    //scroll-view滑到底部触发事件
    async scrollMytrip() {
        try {
            if(this.data.token){
                let result = await this.getTokenStatus();
                if(!this.data.isLogin){
                    my.reLaunch({ url: '../login/login' });
                    return false
                }
            }
            let page = this.data.goodsList.page;
            let list =  this.data.goodsList.list
            // 判断是否还有数据需要加载
            if (list.length < this.data.goodsList.total) {
                this.data.goodsList.show = true;
                let newPage = page + 1;
                this.mySchedulde(newPage);
            }else{
                this.setData({
                    dataEnd:true
                })
            }
        } catch (e) {
            this.data.goodsList.show = false;
            console.log('scrollMytrip执行异常:', e);
        }
    },
    mySchedulde(page = 1) {
        try {
            let url;
            if(this.data.searchKey!=''){//搜索
                url = encodeURI(this.data.api+'/product/search?search='+this.data.searchKey+'&page='+page);
            }else{//返回全部
                url = encodeURI(this.data.api+'/product/search?page='+page);
            }
            let list = this.data.goodsList.list;
            let that = this;
            my.request({
                url:url,
                method: 'get',
                headers:{
                'content-type':'application/json',
                'token':this.data.token
                },
                dataType: 'json',
                success: function(res) {
                    if(res.data.errCode==9){
                        that.setData({
                            goodsList:{
                                total:null,
                                page: null,
                                list:[],
                                show: false
                            },
                            notData:true,
                            dataEnd:false
                        })
                        return 
                    }
                    if(!res.data.data.result[0].level){
                        my.removeStorage({
                            key: 'userInfo'
                        });
                        that.setData({
                            isLogin:false,
                            token:'',
                            dataEnd:false
                        })
                    }
                    if(res.data.errCode==0){
                        let data = res.data.data.result;
                        for (let i = 0; i < data.length; i++) {
                        list.push(data[i]);
                        }
                        that.setData({
                            goodsList:{
                                total:res.data.data.total_count,
                                page: page,
                                list:list,
                                show: false
                            },
                            notData:false,
                            dataEnd:false
                        })
                    }
                },
            });
        } catch (e) {
        console.log('mySchedulde执行异常:', e);
        }
    },
    getTokenStatus(){
        return new Promise((resolve, reject) => {
            let that = this;
            my.request({
                url:this.data.api+'/checkToken',
                method: 'get',
                headers:{
                'content-type':'application/json',
                'token':this.data.token
                },
                dataType: 'json',
                success: function(res) {
                    console.log(res)
                    if(res.data.errCode==9){//token过期
                        my.removeStorage({
                            key: 'userInfo'
                        });
                        that.setData({
                            isLogin:false,
                            token:'',
                            dataEnd:false
                        })
                        my.showToast({
                            type: 'exception',
                            content: res.data.message,
                            duration: 2000
                        });
                    }
                    resolve()
                }
            });
        })
    }
})